const router = require('koa-router')();
const user = require('../app/controller/user');
const serverUtil = require('../utils/serverUtil');
const config = require('../config/config');
const encryptUtil = require('../utils/encryptUtil');

const svgCaptcha = require('svg-captcha');

router.get('login', async function (ctx, next) {

    try {
        let username = ctx.request.query.username;
        let password = ctx.request.query.password;
        let remember = ctx.request.query.remember;
        let verifyCode = ctx.request.query.code;

        console.log('--code--' + ctx.session.verifyCode);

        if (ctx.session.verifyCode && ctx.session.verifyCode != verifyCode) {
            throw new serverUtil.ServerError(serverUtil.code.VERIFY_FAILED);
        }


        if (!username || !password) {
            throw new serverUtil.ServerError(serverUtil.code.ILLEGAL_PARAMS);
        }

        let dbUser = await user.loadUser(username, password);

        if (!dbUser || dbUser.disabled) {
            throw new serverUtil.ServerError(serverUtil.code.FAILED);
        }

        //save login user info to session
        ctx.session.loginUser = dbUser;

        //save auto login info
        if (remember == 1 || remember == true) {
            let cookie_rem = JSON.stringify({
                name: username,
                pwd: password
            });
            ctx.cookies.set(config.REMEMBER_KEY, encryptUtil.encrypt(cookie_rem, config.REMEMBER_SALT), {
                maxAge: 30 * 24 * 60 * 60 * 1000
            });
        }

        ctx.body = {
            code: serverUtil.code.SUCCESS,
            data: dbUser
        }
    } catch (e) {
        let code = e.code || serverUtil.code.UNKNOWN;
        ctx.body = {
            code: code
        }

        serverUtil.logError(ctx, e, "login failed");
    }


});

router.get('loginUser', async function (ctx, next) {
    let sessionUser = ctx.session.loginUser;

    if (sessionUser) {
        
        ctx.body = {
            code: serverUtil.code.SUCCESS,
            data: sessionUser
        }
    } else {
        ctx.body = {
            code: serverUtil.code.UNLOGIN
        }
    }
});

router.get('code', async function (ctx, next) {

    let options = {
        size: 4,
        width: 100,
        height: 50,
        noise: 5,
        color: true
    };

    let q_size = ctx.request.query.size || 4;
    let q_width = ctx.request.query.width || 100;
    let q_height = ctx.request.query.height || 50;

    options = Object.assign(options, {
        width: q_width,
        height: q_height,
        size: q_size
    });

    let captcha = svgCaptcha.create(options);

    ctx.session.verifyCode = captcha.text; // store verify code to session

    ctx.response.type = 'svg';
    ctx.body = captcha.data;


});


router.get('logout', async function (ctx, next) {
    ctx.cookies.set(config.REMEMBER_KEY, '', {
        expire: Date.now() - 10000
    });
    ctx.session = null;
    ctx.redirect('/login.html');
});


module.exports = router;